﻿using System.Windows;
using System.Windows.Controls;

namespace Jerry.DroidBuilder.ThemeEditor.Controls
{
    public class LinearLayout : Grid
    {
        protected override Size MeasureOverride(Size constraint)
        {
            var layoutInfo = DataContext as LayoutInfo;

            if (null != layoutInfo)
            {
                if (layoutInfo.LayoutType == "LinearLayout" || layoutInfo.LayoutType == "TableLayout" || layoutInfo.LayoutType == "TableRow" || layoutInfo.LayoutType == "ScrollView")
                {
                    var orientation = layoutInfo.GetValue("orientation");

                    if (layoutInfo.LayoutType == "TableLayout")
                    {
                        orientation = "vertical";
                    }

                    if (Children.Count > 1)
                    {
                        RowDefinitions.Clear();
                        ColumnDefinitions.Clear();

                        foreach (FrameworkElement child in Children)
                        {
                            var weight = 0;
                            var info = child.DataContext as LayoutInfo;
                            int.TryParse(info.GetValue("layout_weight"), out weight);
                            var index = Children.IndexOf(child);
                            var length = new GridLength(weight, weight > 0 ? GridUnitType.Star : GridUnitType.Auto);

                            if (orientation == "vertical")
                            {
                                SetRow(child, index);
                                RowDefinitions.Add(new RowDefinition { Height = length });
                            }
                            else
                            {
                                SetColumn(child, index);
                                ColumnDefinitions.Add(new ColumnDefinition { Width = length });
                            }
                        }
                    }
                }

                if (Children.Count > 1)
                {
                    foreach (FrameworkElement child in Children)
                    {
                        var info = child.DataContext as LayoutInfo;
                        var layoutWidth = info.GetValue("layout_width");
                        var layoutHeight = info.GetValue("layout_height");

                        if (layoutInfo.LayoutType == "TableLayout")
                        {
                            layoutWidth = "match_parent";
                        }

                        if (layoutWidth == "match_parent" || layoutWidth == "fill_parent")
                        {
                            if (ColumnDefinitions.Count > 0)
                            {
                                SetColumnSpan(child, ColumnDefinitions.Count);
                            }
                        }

                        if (layoutHeight == "match_parent" || layoutHeight == "fill_parent")
                        {
                            if (RowDefinitions.Count > 0)
                            {
                                SetRowSpan(child, RowDefinitions.Count);
                            }
                        }
                    }
                }
            }

            return base.MeasureOverride(constraint);
        }
    }
}
